<template>
  <Card>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-if="hasParentId" @click="handleCreate">新增权限按钮</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleUpdate.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MenuPermsDrawer @register="registerDrawer" @success="handleSuccess" />
  </Card>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive } from 'vue';
  import { Card } from 'ant-design-vue';
  // 引入基础组件
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  // 插入数据内容
  import { subColumns } from './menu.data';
  // 通过API接口获取日志
  import { queryMenuPermission, deletePermsById } from './menu.api';

  import { useDrawer } from '/@/components/Drawer';
  import MenuPermsDrawer from './MenuPermsDrawer.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    components: { Card, BasicTable, MenuPermsDrawer, TableAction },
    setup() {
      const { createMessage } = useMessage();

      let code = ref<string>('');
      let hasParentId = ref<boolean>(false);
      let record = reactive({
        parentId: 0,
      });

      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, setProps }] = useTable({
        title: '权限列表',
        api: queryMenuPermission,
        columns: subColumns,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        immediate: false,
      });

      function filterByParentId(records: Recordable) {
        setProps({ searchInfo: { parentId: records.id } });
        record.parentId = records.id;
        hasParentId.value = true;
        reload();
      }

      function handleCreate() {
        openDrawer(true, { record, isUpdate: false });
      }
      function handleUpdate(record: Recordable) {
        openDrawer(true, { record, isUpdate: true });
      }

      async function handleDelete(record: Recordable) {
        await deletePermsById({ id: record.id }, reload);
        createMessage.success('删除成功!');
        handleSuccess();
      }

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleUpdate,
        handleDelete,
        handleSuccess,
        filterByParentId,
        code,
        hasParentId,
      };
    },
  });
</script>
